<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use app\admin\model\Pic;
use think\db\Where;
use think\Model;

class Article extends Model {
    protected $autoWriteTimestamp = false;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            },'cate'=>function($query){
                $query->field('id,title,icon');
            },'info'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
//        $collection = new Collection();
//        $message = new Message();
        foreach ($list as $k => $v){
//            $list[$k]['message'] = $message->where('aid','eq',$v['id'])->count();
//            $list[$k]['collection'] = $collection->where('aid','eq',$v['id'])->count();
        }
        return $list;
    }

    /**
     * @param $q
     * @param $where
     * @param $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function search($q,$where,$limit){
        $list = $this
            ->field('id,title,cover,cid_type,create_time')
            ->with('info')
            ->where($where)
            ->whereRaw("match(title) against('+{$q}' in boolean mode)")
            ->paginate($limit);
        foreach ($list as &$v){
            if($v['cid_type']==3){
                $v['content'] = $this->getFieldById($v['id'],'content');
            }else{
                $v['content'] = '';
            }
        }
        return $list;
    }


    /**
     * @param int $cate_id
     * @param int $city_id
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lastList($cate_id=0,$city_id=0,$order='create_time desc',$limit=10,$filed='*',$out=false){
        $where = new Where;
        $where['status']=2;
        if($cate_id){
            $where['cid_type']=$cate_id;
        }
        if($city_id){
            $where['city_id']=$city_id;
        }
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            },'cate'=>function($query){
                $query->field('id,title,icon');
            },'info'])
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->select();
        return $list;
    }

    /**
     * @param int $id
     * @param string $filed
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0,$filed='*'){
        $info = $this->field($filed)->find($id);
        $info->hits = $info['hits'] + 1;
        $info->save();
        $res = $this
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type,information');
            },'cate'=>function($query){
                $query->field('id,title,icon');
            },'info'])
            ->where('id','eq',$id)
            ->find();
        return $res;
    }

    /**
     * @param $cid
     * @param int $limit
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function likes($cid,$limit=4){
        $_limit = $limit * 5;
        $sql = "SELECT t1.id,t1.title,t1.cover,t1.create_time FROM `{$this->getTable()}` AS t1 
          JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `{$this->getTable()}`)-(SELECT MIN(id) 
          FROM `{$this->getTable()}`))+(SELECT MIN(id) FROM `{$this->getTable()}`)) AS id from `{$this->getTable()}` limit {$_limit}) AS 
          t2 on t1.id=t2.id where t1.twice_cate_id = {$cid} and t1.status = 2 ORDER BY t1.id LIMIT {$limit}";
        $list = $this->query($sql);
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    public function cate(){
        return $this->hasOne('column','id','twice_cate_id');
    }

    public function info(){
        return $this->hasOne('ArticleInformation','aid','id');
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCoverAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function setContentAttr($value){
        if($value){
            return htmlspecialchars($value);
        }else{
            return '';
        }
    }

    /**
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        if($value){
            return htmlspecialchars_decode($value);
        }else{
            return '';
        }
    }
}